package com.njtoyo.taxi.admin.service.marketing.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.google.common.collect.Lists;
import com.njtoyo.taxi.admin.library.Const;
import com.njtoyo.taxi.admin.library.common.util.CopyUtil;
import com.njtoyo.taxi.admin.mapper.master.entity.ActivityPrized;
import com.njtoyo.taxi.admin.mapper.master.entity.ActivitySeckillAward;
import com.njtoyo.taxi.admin.mapper.master.entity.ActivitySeckillPrize;
import com.njtoyo.taxi.admin.mapper.master.entity.ActivitySeckillRecord;
import com.njtoyo.taxi.admin.mapper.master.enums.ActivityPrizedCategory;
import com.njtoyo.taxi.admin.mapper.master.mapper.ActivityPrizedMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.ActivitySeckillAwardMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.ActivitySeckillPrizeMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.ActivitySeckillRecordMapper;
import com.njtoyo.taxi.admin.rest.presenter.marketing.seckill.PrizeListPresenter;
import com.njtoyo.taxi.admin.rest.wrapper.marketing.seckill.CreatePrizeWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.marketing.seckill.PrizeListWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.marketing.seckill.UpdatePrizeIsOnlineWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.marketing.seckill.UpdatePrizeWrapper;
import com.njtoyo.taxi.admin.service.marketing.SeckillPrizeService;
import com.taxi.entity.common.RestResult;
import com.taxi.entity.common.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;

@Component
@DS(Const.PRIMARY_DS)
public class SeckillPrizeServiceImpl implements SeckillPrizeService {

    @Autowired
    private ActivitySeckillPrizeMapper activitySeckillPrizeMapper;

    @Autowired
    private ActivitySeckillAwardMapper activitySeckillAwardMapper;

    @Autowired
    private ActivityPrizedMapper activityPrizedMapper;

    @Autowired
    private ActivitySeckillRecordMapper activitySeckillRecordMapper;

    @Override
    public RestResult getList(Long awardId, PrizeListWrapper wrapper) {
        ActivitySeckillAward award = activitySeckillAwardMapper.selectById(awardId);
        if (Objects.isNull(award)) {
            return RestResult.build(ResultEnum.NOT_FOUND);
        }
        wrapper.setActivityAwardId(awardId);
        wrapper.setActivityId(award.getActivityId());
        wrapper.setOrders(Lists.newArrayList(new OrderItem("id", false)));

        IPage<PrizeListPresenter> res = activitySeckillPrizeMapper.selectPageJoinCoupon(wrapper);

        return RestResult.success(res);
    }

    @Override
    public RestResult create(Long awardId, CreatePrizeWrapper createPrizeWrapper) {
        ActivitySeckillAward award = activitySeckillAwardMapper.selectById(awardId);
        if (Objects.isNull(award)) {
            return RestResult.build(ResultEnum.NOT_FOUND);
        }
        ActivityPrized seckill = activityPrizedMapper.getActivityById(ActivityPrizedCategory.seckill, award.getActivityId());
        if (Objects.isNull(seckill)) {
            return RestResult.build(ResultEnum.NOT_FOUND);
        }
        if (award.getIsOnline() && !seckill.getIsOfflineUpdate()) {
            return RestResult.failed("请将活动下线后再新增奖品");
        }
        createPrizeWrapper.setActivityAwardId(awardId);
        createPrizeWrapper.setActivityId(award.getActivityId());

        ActivitySeckillPrize prize = CopyUtil.copy(createPrizeWrapper, ActivitySeckillPrize.class);
        prize.setDetails(JSONArray.toJSONString(createPrizeWrapper.getDetails()).toString());
        prize.setCreatedAt(new Date());
        prize.setUpdatedAt(new Date());

        activitySeckillPrizeMapper.insertOne(prize);
        return RestResult.success();
    }

    @Override
    public RestResult update(Long prizeId, UpdatePrizeWrapper updatePrizeWrapper) {
        ActivitySeckillPrize prize = activitySeckillPrizeMapper.selectById(prizeId);
        if (Objects.isNull(prize)) {
            return RestResult.build(ResultEnum.NOT_FOUND);
        }
        ActivitySeckillAward award = activitySeckillAwardMapper.selectById(prize.getActivityAwardId());
        if (Objects.isNull(award)) {
            return RestResult.build(ResultEnum.NOT_FOUND);
        }
        ActivityPrized seckill = activityPrizedMapper.getActivityById(ActivityPrizedCategory.seckill, prize.getActivityId());
        if (Objects.isNull(seckill)) {
            return RestResult.build(ResultEnum.NOT_FOUND);
        }
        if (award.getIsOnline() && !seckill.getIsOfflineUpdate()) {
            return RestResult.failed("请将活动下线后再进行奖品修改");
        }
        ActivitySeckillPrize newPrize = CopyUtil.copy(updatePrizeWrapper, ActivitySeckillPrize.class);
        newPrize.setId(prizeId);
        newPrize.setDetails(JSONArray.toJSONString(updatePrizeWrapper.getDetails()).toString());
        newPrize.setUpdatedAt(new Date());
        activitySeckillPrizeMapper.updateById(newPrize);

        return RestResult.success();
    }

    @Override
    public RestResult delete(Long prizeId) {
        ActivitySeckillPrize prize = activitySeckillPrizeMapper.selectById(prizeId);
        if (Objects.isNull(prize)) {
            return RestResult.build(ResultEnum.NOT_FOUND);
        }
        // 判断是否有抽奖记录
        QueryWrapper<ActivitySeckillRecord> recordWrapper = new QueryWrapper<>();
        recordWrapper.eq("ref_id", prizeId);
        if (activitySeckillRecordMapper.selectCount(recordWrapper) > 0) {
            return RestResult.failed("该奖品已存在参与抽奖记录，无法删除");
        }
        ActivitySeckillAward award = activitySeckillAwardMapper.selectById(prize.getActivityAwardId());
        if (Objects.isNull(award)) {
            return RestResult.build(ResultEnum.NOT_FOUND);
        }
        ActivityPrized seckill = activityPrizedMapper.getActivityById(ActivityPrizedCategory.seckill, prize.getActivityId());
        if (Objects.isNull(seckill)) {
            return RestResult.build(ResultEnum.NOT_FOUND);
        }
        if (award.getIsOnline() && !seckill.getIsOfflineUpdate()) {
            return RestResult.failed("请将活动下线后再删除奖品");
        }
        activitySeckillPrizeMapper.deleteById(prizeId);
        return RestResult.success();
    }

    @Override
    public RestResult updateIsOnline(Long id, UpdatePrizeIsOnlineWrapper updateAwardWrapper) {
        ActivitySeckillPrize prize = activitySeckillPrizeMapper.selectById(id);
        if (Objects.isNull(prize)) {
            return RestResult.build(ResultEnum.NOT_FOUND);
        }

        ActivitySeckillAward award = activitySeckillAwardMapper.selectById(prize.getActivityAwardId());
        if (Objects.isNull(award)) {
            return RestResult.build(ResultEnum.NOT_FOUND);
        }

        ActivityPrized seckill = activityPrizedMapper.getActivityById(ActivityPrizedCategory.seckill, prize.getActivityId());
        if (Objects.isNull(seckill)) {
            return RestResult.build(ResultEnum.NOT_FOUND);
        }

        if (award.getIsOnline() && !seckill.getIsOfflineUpdate()) {
            return RestResult.failed("请将活动下线后再进行奖品修改");
        }

        boolean res = new LambdaUpdateChainWrapper<>(activitySeckillPrizeMapper)
                .eq(ActivitySeckillPrize::getId, id)
                .set(ActivitySeckillPrize::getIsOnline, updateAwardWrapper.getIsOnline())
                .update();

        return RestResult.success(res);
    }
}